﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KhoBanHang
{
    public partial class PhieuNhapKho_DS : Form
    {
        List<Business.PhieuNhapKho> danhSachPhieuNhapKho;
        public PhieuNhapKho_DS()
        {
            InitializeComponent();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (danhSachPhieuNhapKho != null && danhSachPhieuNhapKho.Count > 0)
            {
                if (gridviewDSPhieuNK.SelectedRows.Count == 0)
                {
                    int maPNK = danhSachPhieuNhapKho[0].MaPNK;
                    LoadPhieuNhapKho(maPNK);
                }
                else
                {
                    int maPNK = danhSachPhieuNhapKho[gridviewDSPhieuNK.SelectedRows[0].Index].MaPNK;
                    LoadPhieuNhapKho(maPNK);
                }
            }
        }

       

        private void PhieuNhapKho_DS_Load(object sender, EventArgs e)
        {
            LoadDanhsachPhieuNhapKho(true);
            dtpickerTuNgay.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgay.CustomFormat = "dd/MM/yyyy";
        }

        private void LoadDanhsachPhieuNhapKho(bool lanDau)
        {
            if (lanDau)
            {

                danhSachPhieuNhapKho = Business.PhieuNhapKho.LayDanhSach();
            }
            else
            {

                if (rbtnNgayNhap.Checked && dtpickerTuNgay.Value != null)
                    danhSachPhieuNhapKho = Business.PhieuNhapKho.LayDanhSach_NgayLapPhieu(dtpickerTuNgay.Value, dtpickerDenNgay.Value);
                else if (rbtnMaHD.Checked && txtMaHD.Text.Contains("HDM"))
                    danhSachPhieuNhapKho = Business.PhieuNhapKho.LayDanhSach_MaHoaDonMua(txtMaHD.Text);
                else if (rbtnMaPhieuNhap.Checked && txtMaPhieuNhap.Text.Contains("PNK"))
                    danhSachPhieuNhapKho = Business.PhieuNhapKho.LayDanhSach_MaPhieuNhap(txtMaPhieuNhap.Text);
                else
                    danhSachPhieuNhapKho = Business.PhieuNhapKho.LayDanhSach();
            }

            gridviewDSPhieuNK.DataSource = danhSachPhieuNhapKho;
            gridviewDSPhieuNK.Columns["MaPNK"].Visible = false;

            gridviewDSPhieuNK.Columns["MaHDNhap"].Visible = false;
            gridviewDSPhieuNK.Columns["MaNV"].Visible = false;            
            gridviewDSPhieuNK.Columns["LyDoNhap"].Visible = false;


            gridviewDSPhieuNK.Columns["NgayNhap"].DefaultCellStyle.Format = "dd/MM/yyyy";

            gridviewDSPhieuNK.Columns["MaPNKHienThi"].HeaderText = "Mã phiếu nhập kho";
            gridviewDSPhieuNK.Columns["NgayNhap"].HeaderText = "Ngày lập phiếu";
            gridviewDSPhieuNK.Columns["MaHDNhapHienThi"].HeaderText = "Hóa đơn mua";

            gridviewDSPhieuNK.Columns["MaNVHienThi"].HeaderText = "Nhân viên lập";           

            gridviewDSPhieuNK.Columns["TongGT"].HeaderText = "Tổng GT";
            
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachPhieuNhapKho(false);
        }

        private void LoadPhieuNhapKho(int maPNK)
        {
            PhieuNhapKho form = new PhieuNhapKho();
            form.LayTheoPNK(maPNK);
            form.ShowDialog();
        }
    }
}
